﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using EffiProz.Core;
using EffiProz.Core.Results;
using EffiProz.Core.Persist;
using EffiProz.Core.Navigators;

namespace Sample1
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void btnFile_Click(object sender, RoutedEventArgs e)
        {
            SessionInterface sessionProxy = DatabaseManager.newSession(DatabaseURL.S_FILE,
                "TestDb", "sa", "", new EffiProzProperties(), "", 0);

            string sql = "CREATE TABLE Test(ID int, Name varchar(100));";
            Result resultOut = Result.newExecuteDirectRequest();
            resultOut.setMainString(sql);

            Result resultIn = sessionProxy.execute(resultOut);
            if (resultIn.isError())
            {
                tbkText.Text = "Error in EffiProz";
            }

            sql = "INSERT INTO Test(ID , Name ) VALUES(2,'Bus');";
            resultOut.setMainString(sql);
            resultIn = sessionProxy.execute(resultOut);
            if (resultIn.isError())
            {
                tbkText.Text = "Error in EffiProz";
            }

            sql = "SELECT * FROM TEST;";
            resultOut.setMainString(sql);
            resultIn = sessionProxy.execute(resultOut);
            if (resultIn.isError())
            {
                tbkText.Text = "Error in EffiProz";
            }

            RowSetNavigator nav = resultIn.getNavigator();
            object[] values = nav.getNext();

            tbkText.Text = String.Format("ID = {0}, Name = {1} ", values[0], values[1]);

            sessionProxy.close();
        }

        private void btnMem_Click(object sender, RoutedEventArgs e)
        {
            SessionInterface sessionProxy = DatabaseManager.newSession(DatabaseURL.S_MEM,
                "Test", "sa", "", new EffiProzProperties(), "", 0);

            string sql = "CREATE TABLE Test(ID int, Name varchar(100));";
            Result resultOut = Result.newExecuteDirectRequest();
            resultOut.setMainString(sql);

            Result resultIn = sessionProxy.execute(resultOut);
            if (resultIn.isError())
            {              
                tbkText.Text = "Error in EffiProz";
            }

            sql = "INSERT INTO Test(ID , Name ) VALUES(1,'Car');";
            resultOut.setMainString(sql);
            resultIn = sessionProxy.execute(resultOut);
            if (resultIn.isError())
            {
                tbkText.Text = "Error in EffiProz";
            }

            sql = "SELECT * FROM TEST;";
            resultOut.setMainString(sql);
            resultIn = sessionProxy.execute(resultOut);
            if (resultIn.isError())
            {
                tbkText.Text = "Error in EffiProz";
            }

           RowSetNavigator nav =  resultIn.getNavigator();
           object[] values = nav.getNext();

           tbkText.Text = String.Format("ID = {0}, Name = {1} ", values[0], values[1]);

           sessionProxy.close();
        }
     

       
      
    }
}
